﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace VolunteerManager
{
    /// <summary>
    /// WinLoading.xaml 的交互逻辑
    /// </summary>
    public partial class WinLoading : Window
    {
        public WinLoading()
        {
            InitializeComponent();
            this.txtPassword.Focus();
        }

        private void Label_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        void WinLoading_ContentRendered(object sender, EventArgs e)
        {
            new System.Threading.Thread((x) =>
            {
                Thread.Sleep(200);
                this.Dispatcher.Invoke(new Action(() =>
                {
                    ///TODO 可以此时加载数据
                    MainWindow win = new MainWindow();
                    win.Owner = this;
                    win.Show();
                    this.Hide();
                }));
            }).Start();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {

            if (Model.Config.CheckPass(this.txtPassword.Password))
            {

                this.panelLogin.Visibility = System.Windows.Visibility.Hidden;
                this.txtLoading.Visibility = Visibility.Visible;

                this.WinLoading_ContentRendered(sender, e);
            }
            else
            {
                MessageBox.Show("密码输入错误，请重试", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                this.txtPassword.SelectAll();
            }

        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {

            this.DragMove();
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.btnLogin_Click(sender, e);
            }

        }
    }
}
